
package com.shai.wool.banner.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.shai.wool.banner.entity.BannerConfigVO;
import com.shai.wool.banner.param.BannerConfigParam;
import com.shai.wool.banner.service.BannerConfigService;
import com.shai.wool.common.controller.BaseController;
import com.shai.wool.common.param.SessionUserInfo;
import com.shai.wool.sensitiveword.SensitiveWordUtils;

/**
 * banner配置Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class BannerConfigController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(BannerConfigController.class);
    
    @Autowired
    private BannerConfigService bannerConfigService;
    
    private final static String LIST_PAGE = "/banner";

    private final static String ADMIN_LIST_PAGE = "/admin/banner_list";
    
    private final static String UPDATE_PAGE = "/admin/banner_update";

    @RequestMapping(value = "/admin/banner/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        BannerConfigVO bannerConfigVO = new BannerConfigVO();
        resultMap.put("banner", bannerConfigVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/banner/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        BannerConfigVO bannerConfigVO = bannerConfigService.findById(id);
        resultMap.put("banner", bannerConfigVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/admin/banner/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(BannerConfigVO banner) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        
        if (SensitiveWordUtils.isContaintSensitiveWord(banner.getBannerName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "banner名称中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(banner.getBannerName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }

        if (SensitiveWordUtils.isContaintSensitiveWord(banner.getBannerUrl(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "banner链接地址中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(banner.getBannerUrl(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        try {
            Integer affectCount = 0;
            if (banner != null && StringUtils.isNotBlank(banner.getBannerId())) {
                affectCount = bannerConfigService.update(banner);
            } else if (banner != null) {
                banner.setBannerId(UUID.randomUUID().toString());
                banner.setDataStatus(0);
                banner.setPublishUser(seUser.getUserId());
                affectCount = bannerConfigService.insert(banner);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改banner配置成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改banner配置失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改banner配置出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改banner配置出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/banner/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = bannerConfigService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除banner配置成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除banner配置失败");
            }
        } catch (Exception e) {
            logger.error("删除banner配置出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除banner配置出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/uncheck/banner/list")
    public ModelAndView queryList() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<BannerConfigVO> dataList = bannerConfigService.queryBannerList();
        resultMap.put("bannerList", dataList);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/admin/banner/list")
    public ModelAndView queryAdminList(BannerConfigParam bannerConfigParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<BannerConfigVO> dataList = bannerConfigService.queryByPage(bannerConfigParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", bannerConfigParam);
        return new ModelAndView(ADMIN_LIST_PAGE, resultMap);
    }
}
